﻿

Imports System.Net
Imports System.Text
Imports System.Web
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Imports System.Security.Cryptography
Imports Microsoft.Win32
Imports System.Windows.Forms

Public Class MyWowheadReader
    Private wb As New WebBrowser
    Private doc As mshtml.HTMLDocument
    '
    Public Sub New()
        wb.ScriptErrorsSuppressed = True
        wb.AllowNavigation = True
        wb.Navigate("about:blank")
    End Sub
    '
    Public Function GetScriptElementsFromUrl(Url As String) As ArrayList
        Dim r As New ArrayList
        Try
            Dim client As WebClient = New WebClient()
            Dim s As String = client.DownloadString(Url)
            Dim doc1 As System.Windows.Forms.HtmlDocument = wb.Document.OpenNew(True)
            doc1.Write(s)
            Application.DoEvents()
            doc = doc1.DomDocument
            Dim js As mshtml.IHTMLElementCollection = doc.getElementsByTagName("script")
            For Each e As mshtml.IHTMLElement In js
                If InStr(e.innerHTML, "template:") > 0 And InStr(e.innerHTML, "id:") > 0 Then
                    r.Add(e)
                End If
            Next
        Catch ex As Exception
            Stop
        End Try
        Return r
    End Function

    Public Shared Function CleanJavaString(Text As String) As String
        Text = "{" & GetMidString(Text, "{", ");")

        Text = Replace(Text, "name: LANG.tab_npcs,", "")
        Text = Replace(Text, "name: LANG.tab_zones,", "")
        Text = Replace(Text, "name: LANG.tab_objects,", "")
        Text = Replace(Text, "name: LANG.tab_quests,", "")
        Text = Replace(Text, "name: LANG.tab_startsquest,", "")
        Text = Replace(Text, "name: LANG.tab_achievements,", "")
        Text = Replace(Text, "name: LANG.tab_fishing,", "")
        Text = Replace(Text, "name: LANG.tab_drops,", "")
        Text = Replace(Text, "name: LANG.tab_items,", "")
        Text = Replace(Text, "name: LANG.tab_questrewards,", "")

        Text = Replace(Text, "tabs: tabsRelated,", "")
        Text = Replace(Text, "undefined", "'undefined'")

        Text = Replace(Text, "sort:['-percent', 'name'],", "")
        Text = Replace(Text, "computeDataFunc: Listview.funcBox.initLootTable,", "")
        Text = Replace(Text, "_totalCount: 130107,", "")

        Dim s2 As String = GetMidString(Text, "note: ", "),")
        Text = Replace(Text, "note: " & s2 & "),", "")

        s2 = GetMidString(Text, "extraCols: [", "],")
        Text = Replace(Text, "extraCols: [" & s2 & "],", "")

        s2 = GetMidString(Text, "computeDataFunc: ", ",")
        Text = Replace(Text, "computeDataFunc: " & s2 & ",", "")

        s2 = GetMidString(Text, "onAfterCreate: ", ",")
        Text = Replace(Text, "onAfterCreate: " & s2 & ",", "")
        Return Text
    End Function
    Private Shared Function GetMidString(ByVal Text As String, ByVal DelimeryLeft As String, ByVal DelimeryRight As String) As String
        Dim s1 As String = ""
        Try
            If String.IsNullOrEmpty(Text) Then Return ""
            Dim x1 As Integer = InStr(Text.ToLower, DelimeryLeft.ToLower)
            If x1 = 0 Then
                s1 = Text
            Else
                s1 = Mid(Text, x1 + Len(DelimeryLeft))
            End If
            If String.IsNullOrEmpty(DelimeryRight) Then
                Return s1
            End If
            Dim x2 As Integer = InStr(s1, DelimeryRight.ToLower)
            If x2 = 0 Then
                Return s1
            Else
                s1 = Mid(s1, 1, x2 - 1)
            End If
            Return s1
        Catch ex As Exception
            Stop
        End Try
        Return ""
    End Function



End Class

